<!--
 * @Author: 刘晟勇
 * @Date: 2021-10-12 21:35:45
 * @LastEditTime: 2021-10-22 13:33:34
 * @Description: 首页页面
-->
<template>
  <div class="home-container">
    <!-- 头部区域 -->
    <!-- 引用HeaderNav组件 -->
    <HeaderNav class="home-header wrap" :active="headerActive">
      <template #icon>
        <i class="iconfont icon-caidan"></i>
      </template>
      <template #center>
        <div class="header-search">
          <span class="app-name">G</span>
          <i class="iconfont icon-search"></i>
          <span class="search-title">家电返场同价11.11</span>
        </div>
      </template>
      <template #right>
        <router-link to="/login" tag="span" v-if="!isLogin || isLogin === 'null'">登录</router-link>
        <router-link tag="span" to="/user" v-else>
          <i class="iconfont icon-iconyonghu"></i>
        </router-link>
      </template>
    </HeaderNav>

    <!-- 轮播图区域 -->
    <van-swipe
      :autoplay="3000"
      indicator-color="white"
      class="swiper-container"
    >
      <van-swipe-item v-for="item in swipeList" :key="item.categoryId">
        <img :src="item.imgUrl" class="swiper_img" alt="" />
      </van-swipe-item>
    </van-swipe>

    <!-- 首页分类 -->
    <section class="category-list">
      <div
        v-for="(item, index) in CategoryList"
        :key="index"
        @click="ToProductList(item.categoryId)"
      >
        <img :src="item.imgUrl" />
        <span>{{ item.name }}</span>
      </div>
    </section>

    <!-- 首页购物楼层 -->
    <section class="floor-list">
      <div class="floor-wrap" v-for="(item, index) in floorList" :key="index">
        <img :src="item.headUrl" class="floor-head" />
        <div class="floor-content">
          <div
            class="floor-category"
            v-for="(floor, index) in item.list"
            :key="index"
          >
            <p>{{ floor.title }}</p>
            <p>{{ floor.desc }}</p>
            <div class="floor-products">
              <img
                :src="floorImg.imgUrl"
                v-for="(floorImg, index) in floor.products"
                :key="index"
              />
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 引用TabBar组件 -->
    <TabBar />
  </div>
</template>

<script>
// 公共组件的引用
import TabBar from "@/components/tabBar";
import HeaderNav from "@/components/header-nav.vue";
// 导入混入api方法
import { getSwipeList, getCategoryList, getFloorList } from "@/api/home"
import { mapState } from 'vuex'

export default {
  name: "Home",
  components: {
    TabBar,
    HeaderNav,
  },
  data() {
    return {
      // 是否激活header
      headerActive: false,
      // 轮播图列表
      swipeList: [],
      // 分类列表
      CategoryList: [],
      // 商品列表
      floorList: [],
    };
  },
  computed: {
    ...mapState(['isLogin'])
  },
  created() {
    // 执行获取轮播图列表方法
    getSwipeList((data) => {
      this.swipeList = data;
    });
    // 执行获取分类列表方法
    getCategoryList((data) => {
      this.CategoryList = data;
    });
    // 执行获取商品列表方法
    getFloorList((data) => {
      this.floorList = data;
    });
  },
  mounted() {
    // 添加滚动事件
    window.addEventListener("scroll", this.pageScroll);
  },
  methods: {
    // 跳转到商品列表
    ToProductList(id) {
      this.$router.push({
        name: "Product-list",
        params: {
          id,
        },
      });
    },
    // 滚动事件回调
    pageScroll() {
      // 获取滚动距离
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop > 100
        ? (this.headerActive = true)
        : (this.headerActive = false);
    },
  },
};
</script>

<style lang="scss" scoped>
// 头部区域样式
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  color: #fff;

  & header {
    @include fj;
  }

  &.active {
    background: $red;
  }

  .icon-caidan {
    font-size: 50px;
  }

  .header-search {
    display: flex;
    width: 74%;
    height: 40px;
    line-height: 40px;
    margin: 20px 0;
    padding: 10px 0;
    color: #232326;
    background: #fff;
    @include borderRadius(40px);

    .app-name {
      padding: 0 20px;
      color: $red;
      font-size: 40px;
      font-weight: bold;
      border-right: 1px solid #666;
    }

    .icon-search {
      padding: 0 20px;
      font-size: 34px;
    }

    .search-title {
      font-size: 24px;
      color: #666;
      background: #fff;
    }
  }

  .icon-iconyonghu {
    font-size: 44px;
  }
}
// 轮播图样式
.swiper-container {
  .swiper_img {
    width: 100%;
  }
}
// 首页分类样式
.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 26px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      width: 80px;
      height: 80px;
      margin: 26px auto 16px auto;
    }
  }
}
// 首页购物楼层样式
.floor-list {
  width: 100%;
  padding-bottom: 100px;

  .floor-head {
    width: 100%;
    height: 80px;
    background: #f6f6f6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    @include boxSizing;

    .floor-category {
      width: 50%;
      padding: 20px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      @include boxSizing;

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 34px;
        color: #333;

        &:nth-child(2) {
          padding: 10px 0;
          font-size: 26px;
          color: $red;
        }
      }

      .floor-products {
        display: flex;
        justify-content: space-around;
        width: 100%;

        img {
          width: 130px;
          height: 130px;
        }
      }
    }
  }
}
</style>
